﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using YT.Commons;
using YT.DB;

namespace YT.XH.MeniereDisease
{
    public partial class frmMDTreatmentEffectMtn : Form
    {
        private string mstrSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        public frmMDTreatmentEffectMtn()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
        }
        public frmMDTreatmentEffectMtn(string strSickRecID)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
        }
        private void FillBaseInfo()
        {
            string strSQL = string.Empty;
            strSQL = "select * from mSickPerson where sickRecID='" + mstrSickRecID + "'";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                txtSickName.Text = CvtFunc.ToString(drRow["SickName"]);
                txtAddress.Text = CvtFunc.ToString(drRow["Address"]);
                txtPhone.Text = CvtFunc.ToString(drRow["Phone"]) +"  " + CvtFunc.ToString(drRow["MobilePhone"]);
                txtRemark.Text = CvtFunc.ToString(drRow["Remark"]);
            }

        }

        private bool FillDetail()
        {
            this.dgvDetail.Rows.Clear();
            string strSQL = string.Empty;
            int intRow = 0;
            strSQL = "select * from TreatmentEffectHdr where sickRecID='" + mstrSickRecID + "' and Type='" + TitleType.MeniereDisease + "'  order by SeqNo";
            DataTable dtDtl = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtDtl.Rows)
            {
                dgvDetail.Rows.Add();
                this.dgvDetail.Rows[intRow].Cells[colDate.Index].Value = BaseFunctions.FormatDate(CvtFunc.ToString(drRow["ExamDate"]), DateFormatOption.DateFmtYMD);
                this.dgvDetail.Rows[intRow].Cells[ColSeqNo.Index].Value = CvtFunc.ToString(drRow["SeqNo"]);
                this.dgvDetail.Rows[intRow].Cells[colRecID.Index].Value = CvtFunc.ToString(drRow["RecID"]);
                this.dgvDetail.Rows[intRow].Cells[colRemark.Index].Value = CvtFunc.ToString(drRow["Remark"]);
                this.dgvDetail.Rows[intRow].Cells[ColCaseName.Index].Value = CvtFunc.ToString(drRow["CaseName"]);    
                intRow++;
            }
            return true;
        }

        private void frmMDTreatmentEffectMtn_Load(object sender, EventArgs e)
        {
            this.Text = "疗效评估";
            FillBaseInfo();

            if (DataFunctions.GetFieldValue("mTreatHistory", "TreatCaseName", " SickRecID='" + mstrSickRecID + "'") != "")
                FillDetail();
            else
            {
                this.Close();
                MessageBox.Show("请先选择 治疗方案", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            timer1.Start();
        }

        private void dgvDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == colDetail.Index)
            {
                ShowMtn();
            }  
        }

        private void ShowMtn()
        {
            string strSeqNo = string.Empty;
            strSeqNo = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[ColSeqNo.Index].Value);

            //这里写要填写的具体内容,就是“治疗前的MTN"
            FormStatus mfrmStatus = new FormStatus();
            mfrmStatus = FormStatus.Update;

            frmMDTreatmentEffectTitle frmBefore = new frmMDTreatmentEffectTitle(mstrSickRecID, mfrmStatus, strSeqNo);
            frmBefore.Width = 700;
            frmBefore.Height = 600;
            frmBefore.StartPosition = FormStartPosition.CenterScreen;
            frmBefore.ShowDialog();
            FillDetail();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            FormStatus mfrmStatus = new FormStatus();
            mfrmStatus = FormStatus.AddNew;
            frmMDTreatmentEffectTitle frmBefore = new frmMDTreatmentEffectTitle(mstrSickRecID, mfrmStatus);
            frmBefore.ShowDialog();
            FillDetail();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmMDTreatmentEffectMtn_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (UpdateHdr()) this.Close();
        }

        private bool UpdateHdr()
        {
            this.dgvDetail.EndEdit();
            string strSQL = string.Empty;
            foreach (DataGridViewRow dgvRow in this.dgvDetail.Rows)
            {
                string strRemark= CvtFunc.ToString ( dgvRow.Cells [colRemark.Index ]. Value );
                string strRecID=CvtFunc.ToString ( dgvRow .Cells [colRecID.Index].Value );
                strSQL ="update TreatmentEffectHdr set Remark='"+ strRemark +"' where RecID='"+ strRecID +"' ";
                mdbcon.ExecuteSql (strSQL );
            }
            return true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

    }
}
